<template>

<el-container>
  <el-header>用户登录</el-header>
  <el-main><el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码"   prop="password">
        <el-input type="password"  v-model="ruleForm.password"></el-input>
      </el-form-item>
      
        <el-button type="primary" @click="submitForm(ruleForm)"
          >立即登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
       
       <el-button @click="toRegister">注册用户</el-button>
    </el-form></el-main>
  <el-footer>powerBy Edwin</el-footer>
</el-container>
    
    
  
</template>

<script>
import UserApi from '@/api/UserApi'
export default {
  name:"LoginComponent", 
  data() {
    return {
      ruleForm: {
        username:"",
        password:"",     
      }
    };
  },
  methods: {
    toRegister(){
      this.$router.push("/register")
    },
    submitForm(ruleForm) {
         
           UserApi.login(ruleForm).then(result=>{
            console.log("result：",result);
            console.log("====",result);
          if(result.code ==200){
            console.log("登录成功");
            console.log(result.msg);
            this.setToken(result.msg)
            this.$router.push("/main")
          }else{
            alert(result.msg)
            console.log(result.msg);
          }
           })
          
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
// 设置token
    setToken(token){
      localStorage.removeItem("token")
      localStorage.setItem("token",token)
    }
  },
};
</script>

<style>
 .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }
</style>